#import('dart:html');
#source('Timer.dart');

class Bar {

  num clockTick;
  Timer timer;
  int count = 0;
  int animationStepCount = 0;
  var shakeFrames;
  var rightPourFrames;
  var leftPourFrames;
  var putAwayFrames;
  var activeAnimation = "";
  var animationCurrentFrame = 0;
  var bartopFrontOn = true;
  var itemSelected = 0;
  
  Bar() {
    timer = new Timer();

    shakeFrames =  [
      "putdown1.png",
      "putdown2.png",
      "putdown3.png",
      "putdown2.png",
      "putdown3.png",
      "putdown2.png",
      "putdown1.png",
    ];
    
    rightPourFrames = [[
      "right11.png",
      "right12.png",
      "right13.png",
      "right14.png",
      "right15.png",
      "right14.png",
      "right13.png",
      "right12.png",
      "right11.png",
],[
      "right21.png",
      "right22.png",
      "right23.png",
      "right24.png",
      "right25.png",
      "right24.png",
      "right23.png",
      "right22.png",
      "right21.png",
],[
      "right31.png"
      "right32.png",
      "right33.png",
      "right34.png",
      "right35.png",
      "right34.png",
      "right33.png",
      "right32.png",
      "right31.png",
]];   

    leftPourFrames = [
                      "leftpour1.png",
                      "leftpour2.png",
                      "leftpour3.png",
                      "leftpour4.png",
                      "leftpour5.png",
                      "leftpour4.png",
                      "leftpour3.png",
                      "leftpour2.png",
                      "leftpour1.png",
              ];
    
    putAwayFrames = [
                     "putdown1.png",
                     "putdown2.png",
                     "putdown3.png", // Arm down
                     "putdown4.png",
                     "putdown5.png",
                     "putdown6.png",
                     "putdown7.png", // Arm up
                     "putdown8.png",
                     "putdown9.png",
                     "putdown10.png", 
                     0, 
                     "putdown9.png",// Glass up
                     "putdown8.png", 
                     "putdown7.png",
                     "putdown6.png",
                     //1,
                     "armup2.png",
                     "armup3.png",
                     "armup4.png",
                     "armup5.png",
                     0,
                     "armup5.png",
                     "armup4.png",
                     "armup3.png",
                     "armup2.png",
                     "armup1.png",
                     "putdown6.png",
                     "putdown5.png",
                     "putdown4.png",
                     "putdown3.png",
                     "putdown2.png",
                     "putdown1.png",
                     //1,
             ];    
    // Load all the images the hacky way.
    var text = "";
    for(int i=0; i<shakeFrames.length; i++)
    {
      text += "<img src='images/"+shakeFrames[0]+"' />";
 
    }
//    document.query('#imagebank').innerHTML = text;    
    
    activeAnimation = "shaker";
  }

  bool animatePour(int frame)
  {
    var leftarm = document.query("#leftarm");
    leftarm.style.position = "absolute";
    leftarm.style.left = "395px";
    leftarm.style.top = "200px";
    leftarm.src = "images/"+leftPourFrames[frame];
    
    var rightarm = document.query("#rightarm");
    rightarm.style.position = "absolute";
    rightarm.style.left = "110px";
    rightarm.style.top = "150px";
    rightarm.src = "images/"+rightPourFrames[0][frame]; 

    // If we are at the last frame, return false to let the animator know the animation has compelted.
    if((frame+1) == rightPourFrames[0].length)
    {
      return false;
    }
    else
    {
      return true;
    }

  }

  bool animatePush(int frame)
  {
    var leftarm = document.query("#leftarm");
    leftarm.style.position = "absolute";
    leftarm.style.left = "445px";
    leftarm.style.top = "75px";
    
    document.query("#bartopfront").style.display = (bartopFrontOn) ? "display" : "none";
    
    if(putAwayFrames[frame] == 0)
    {
      leftarm.src = "images/"+putAwayFrames[0];
      bartopFrontOn != bartopFrontOn;
    }
    else
    {
      leftarm.src = "images/"+putAwayFrames[frame];
    }
    
    var rightarm = document.query("#rightarm");
    rightarm.style.position = "absolute";
    rightarm.style.left = "65px";
    rightarm.style.top = "160px";
    rightarm.src = "righarm3.gif";
    
// If we are at the last frame, return false to let the animator know the animation has compelted.
    if((frame+1) == putAwayFrames.length)
    {
      return false;
    }
    else
    {
      return true;
    }
  }
  
  // Returns false if animation is complete, true if we want to be called again next time.
  bool animateShaker(int frame)
  {
    print("animateShaker(): frame="+frame);
    var leftarm = document.query("#leftarm");
    leftarm.style.position = "absolute";
    leftarm.style.left = "440px";
    leftarm.style.top = "77px";
    leftarm.src = "./images/"+shakeFrames[frame];
    
    var rightarm = document.query("#rightarm");
    rightarm.style.position = "absolute";
    rightarm.style.left = "110px";
    rightarm.style.top = "145px";
    rightarm.src = "images/"+rightPourFrames[0][0];
    
    // If we are at the last frame, return false to let the animator know the animation has compelted.
    if((frame+1) == shakeFrames.length)
    {
      return false;
    }
    else
    {
      return true;
    }
  }    

  String convertCommand(String command) {
    if (command == 's') {
      return "shaker";
    } else if (command == 'p') {
      return "push";
    } else if (command == 'o6') {
      return "pour";
    } else if (command == 'i1') {
      return "i1";
    } else if (command == 'i2') {
      return "i2";
    } else if (command == 'i3') {
      return "i3";
    } else {
      print("wrong command="+command);
    }
  }
  
  void run() {
    WebSocket ws = new WebSocket("ws://192.168.1.13:8090/ws");
    
    ws.on.open.add((event) {
      bool ret = ws.send("hello from solomon");
      print("send: "+ret);
    });
    
    
    ws.on.message.add((event) {
      print("receive something from websocket: "+event.data);
      
      String command = event.data.trim();
      if (event.data == "i0") {
        this.itemSelected = 0;
      } else if (event.data == "i1") {
        this.itemSelected = 1;
      } else if (event.data == "i2") {
        this.itemSelected = 2;
      } else {
        print("this.activeAnimation="+this.activeAnimation);
        String animationType = convertCommand(command);
        if (this.activeAnimation == "") {
          this.animationStepCount = 0;
          this.animationCurrentFrame = 0;
          this.activeAnimation = animationType;
        }
      }
      
      //if (this.activeAnimation == "") {
      //this.activeAnimation = "shaker";
      //}
    });
    
    
    
    var torso = document.query("#torso");
    torso.style.position = "absolute";
    torso.style.left="300px";
    
    var bartopback = document.query("#bartopback");
    bartopback.src = "http://dartender.googlecode.com/svn/trunk/simple_test/bartopgif.gif";;
    //document.query("#rightarm").src = "http://dartender.googlecode.com/svn/trunk/simple_test/rightPour1.gif";

    var bartopfront = document.query("#bartopfront");
    bartopfront.src = "http://dartender.googlecode.com/svn/trunk/simple_test/bartopgif.gif";;
    
    
    var leftarm = document.query("#leftarm");
    leftarm.style.position = "absolute";
    leftarm.style.left = "440px";
    leftarm.style.top = "77px";
    leftarm.src = "./images/"+shakeFrames[0];
       
    var rightarm = document.query("#rightarm");
    rightarm.style.position = "absolute";
    rightarm.style.left = "65px";
    rightarm.style.top = "160px";
    rightarm.src = "./images/"+rightPourFrames[0];

    
    //runShakeAnimation();
    
    
    
  }

  void write(String message) {
    // the HTML library defines a global "document" variable
    document.query('#status').innerHTML = message;
  }
  
  void start() {
    print("starting game");
    count = 0;
    this.animationStepCount = 0;
    window.webkitRequestAnimationFrame(loop);
  }
  
  /**
   * this is called 60 times/sec
   */
  void loop(int time) {
    clockTick = this.timer.tick();
    this.count++;
    //print("loop: count="+count+".  time="+time);
    
    //document.query('#status').innerHTML = "frame count="+count.toString() + ".  animation step count="+this.animationStepCount.toString();
    
    final int numAnimationFramePerSecond= 60; // number of animation frame per second. the number of times loop() is called per second.  
    int numAnimationStepPerSecond = 3; // number of animation steps per second
    
    int numberOfFrameToSkip = (numAnimationFramePerSecond ~/ numAnimationStepPerSecond);  // skip 20 frames.  for each 20 frames, it is one animation step.  
    // or (60 / 3).toInt()
    
    if (this.count % numberOfFrameToSkip == 0) {
      if (this.activeAnimation != "") {
        this.animationStepCount++;
      }
      print("animation step count="+this.animationStepCount);
      runAnimationStep();
    }

    window.webkitRequestAnimationFrame(loop);
  }
  
  /**
   * run one animation step
   */
  void runAnimationStep() {
    document.query('#status').innerHTML = "Animation |"+activeAnimation+"|"+this.animationStepCount;
    
    if(activeAnimation != "")
    {
      // There's an animation to be performed.
      var continueanimation = false;
      
      if(activeAnimation == "shaker")
      {
        continueanimation = animateShaker(this.animationStepCount);
      }
      
      if(activeAnimation == "pour")
      {
        continueanimation = animatePour(this.animationStepCount);
      }
      
      if(activeAnimation == "push")
      {
        continueanimation = animatePush(this.animationStepCount);
      }
      
      
  
      if(!continueanimation)
      {
        activeAnimation = "";
        this.animationStepCount = 0;
      }
    }

    // content goes here.
  }
}

void main() {
  Bar bar = new Bar();
  bar.run();
  bar.start();
  
  }
